//
//  TRSearchProductView.m
//  WeiMiLan
//
//  Created by anan on 14/12/10.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRSearchEditView.h"

@implementation TRSearchEditView

- (void)reload
{
  //  _tb.hidden=YES;
    self.directorys=@[@{@"ID":@"",@"NAME":@"全部"},@{@"ID":@"2219",@"NAME":@"服装"},@{@"ID":@"2218",@"NAME":@"包包"},@{@"ID":@"2217",@"NAME":@"丝巾"},@{@"ID":@"2216",@"NAME":@"手表"},@{@"ID":@"2215",@"NAME":@"鞋子"},@{@"ID":@"2214",@"NAME":@"眼镜"},@{@"ID":@"2213",@"NAME":@"皮带"},@{@"ID":@"2212",@"NAME":@"钱夹"},@{@"ID":@"2211",@"NAME":@"饰品"}];
   // [self initSeachButton];
    
    
    [self updateKeyWordsUI:@[@"时尚",@"精品",@"美丽"]];
    
    [self seachRequest];
    
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self addGestureRecognizer:tapGr];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchButtonClicked:) name:@"searchBarSearchButtonClicked" object:nil];
  //   [self.seachWordTF resignFirstResponder];
}
- (void)searchButtonClicked: (NSNotification *)noti {
    NSLog(@"%@",noti);
    NSLog(@"－－－－－接收到通知------");
    
    NSDictionary *dict = noti.userInfo;
    NSString *searchText = dict[@"text"];
    [self seachAction:searchText];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
    [self.seachWordTF resignFirstResponder];
}


-(void)seachRequest{
    [ApplicationDelegate.webAPI getWebAPIWithParams:@{TYPE:@"1020"} andMethodNode:NODE_DIRECTORY completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
        if (state ==1) {
            
            if ([responseObject[@"MESSAGE_INFO"][@"CODE"] isEqualToString:@"0000"]) {
                NSArray * arr = responseObject[@"PACKAGE_RSP"][@"PACKAGE"];
                _titlesArray = [@[]mutableCopy];
                
                for (NSDictionary * nameArr in arr) {
                    [_titlesArray addObject:nameArr[@"NAME"]];
                }
                
                [self seachHotWord];
            }
            
        }
    }];
}

-(void)seachHotWord
{
    NSMutableArray *colorArray = [@[[UIColor greenColor],[UIColor blueColor],[UIColor purpleColor],[UIColor redColor],[UIColor grayColor],[UIColor magentaColor],[UIColor brownColor],[UIColor yellowColor],[UIColor cyanColor]]mutableCopy];
    NSMutableArray *frameArray = [[NSMutableArray alloc] initWithObjects:@"{{54, 111}, {200, 70}}",@"{{26, 228}, {120, 70}}",@"{{132, 124}, {50, 70}}",@"{{64, 146}, {120, 80}}",@"{{180, 175}, {420, 70}}",@"{{54, 190}, {120, 40}}",@"{{132, 238}, {44, 70}}",@"{{170, 269}, {200, 70}}",@"{{47, 290}, {10, 70}}", nil];
    //如果文字给颜色多，颜色就自我复制
    while (_titlesArray.count>colorArray.count) {
        [colorArray addObjectsFromArray:colorArray];
    }
    NSMutableArray * labelArr = [@[]mutableCopy];
    
    
    for (int i = 0 ;i<_titlesArray.count;i++) {
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectZero];
        
        label.text = [_titlesArray objectAtIndex:i];
        int num = arc4random()%[colorArray count];
        label.textColor = colorArray[num];
        [colorArray removeObjectAtIndex:num];
        label.font = [UIFont boldSystemFontOfSize:k_FontSize];
        [self addSubview:label];
        
        label.center = self.center;
        [labelArr addObject:label];
        label.userInteractionEnabled = YES;
        //加手势
        UITapGestureRecognizer * tapVc = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didselectText:)];
        [label addGestureRecognizer:tapVc];
    }
    
    
    NSString * frameString;
    while (_titlesArray.count>=frameArray.count) {
        
        
        int w = arc4random()%100+44;
        int h = arc4random()%100+100;
        int x=arc4random()%(int)(320-w/2)+w/2;
        int y= arc4random()%(int)(568-h/2-44)+100;
        frameString = [NSString stringWithFormat:@"{{%d, %d}, {%d, %d}}",x,y,w,h];
        
     //   NSLog(@"%@",frameString);
        if (![frameArray containsObject:frameString]) {
            [frameArray addObject:frameString];
        }
        
        
        
    }
    
    for (int i = 0 ;i<labelArr.count;i++) {
        
        UIView *label = labelArr[i];
        
        if ([label isMemberOfClass:[UILabel class]]) {
            int rand =arc4random()%[frameArray count];
            [UIView animateWithDuration:2 animations:^{
                
                label.frame = CGRectFromString(frameArray[rand]);
                NSLog(@"%@",frameArray[rand]);
                
            } completion:nil];
            [label sizeToFit];
            [frameArray removeObjectAtIndex:rand];
            
        }
        
    }
}

- (void)seachAction:(NSString *)sender
{
    [self viewTapped:nil];
    
    
    TRsearchViewController * VC=[[QLPlayerManager getStoryboard] instantiateViewControllerWithIdentifier:@"TRsearchViewController"];
    VC.searchString=sender;
    
    VC.searchInString=self.directorys[self.selectIndex][@"ID"];
    
    
    [self.viewController.navigationController pushViewController:VC animated:YES];
    
    
}
-(void)didselectText:(UITapGestureRecognizer*)tap{
    UILabel * label = (UILabel *)tap.view;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didselectedText:selectText:)]) {
        [self.delegate didselectedText:self selectText:label.text];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"didselectText" object:nil userInfo:@{@"text": label.text, @"label": label}];
    
 
    
}
-(void)updateKeyWordsUI:(NSArray *)keyWords
{
    KYRecentSearchView *recentSearchView = [[KYRecentSearchView alloc] initWithRecentSearches:keyWords];
    recentSearchView.frame = CGRectMake(0, 80, 320, 100);
}

-(void)exit
{
    [self.viewController.navigationController popViewControllerAnimated:YES];
}


-(void)viewTapped:(UITapGestureRecognizer*)tapGr{
    [self.seachWordTF resignFirstResponder];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"viewTapped" object:nil userInfo:nil];
  //  [self.inputTextField resignFirstResponder];
}

@end
